package db.abstr.facades;

import java.util.List;
import javax.ejb.Remote;
import javax.persistence.EntityManager;

/**
 *
 * @author E589510
 */
@Remote
public abstract class AbstractFacade<T> implements IAbstractFacade<T>
{
    private Class<T> entityClass;

    public AbstractFacade(Class<T> entityClass)
    {
            this.entityClass = entityClass;
    }

    protected abstract EntityManager getEntityManager();

    @Override
    public void create(T entity)
    {
            getEntityManager().persist(entity);
    }

    @Override
    public void edit(T entity)
    {
            getEntityManager().merge(entity);
    }

    @Override
    public void remove(T entity)
    {
            getEntityManager().remove(getEntityManager().merge(entity));
    }

    @Override
    public T find(Object id)
    {
            return getEntityManager().find(entityClass, id);
    }

    @Override
    public List<T> findAll()
    {
            javax.persistence.criteria.CriteriaQuery cq = getEntityManager().getCriteriaBuilder().createQuery();
            cq.select(cq.from(entityClass));
            return getEntityManager().createQuery(cq).getResultList();
    }

    @Override
    public List<T> findRange(int[] range)
    {
            javax.persistence.criteria.CriteriaQuery cq = getEntityManager().getCriteriaBuilder().createQuery();
            cq.select(cq.from(entityClass));
            javax.persistence.Query q = getEntityManager().createQuery(cq);
            q.setMaxResults(range[1] - range[0] + 1);
            q.setFirstResult(range[0]);
            return q.getResultList();
    }

    @Override
    public int count()
    {
            javax.persistence.criteria.CriteriaQuery cq = getEntityManager().getCriteriaBuilder().createQuery();
            javax.persistence.criteria.Root<T> rt = cq.from(entityClass);
            cq.select(getEntityManager().getCriteriaBuilder().count(rt));
            javax.persistence.Query q = getEntityManager().createQuery(cq);
            return ((Long) q.getSingleResult()).intValue();
    }
	
}
